import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/utils/location_test_page.dart';
import 'package:enterprise_v6_app/app/core/utils/user_manager.dart';
import 'package:enterprise_v6_app/app/modules/profile/controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 首页 我的
class MyPagePage extends StatelessWidget {
  MyPagePage({super.key});

  final controller = Get.put(HomeMyController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Color.fromARGB(255, 83, 136, 250),
                  Colors.white,
                  Colors.white,
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
          ),
          ListView(
            physics: const BouncingScrollPhysics(),
            children: [
              AppGaps.vGap20,
              GestureDetector(
                onLongPress: () {
                  Get.to(LocationTestPage.new);
                },
                child: Text(
                  '我的',
                  style: TextStyle(
                    fontSize: 18.w,
                    fontWeight: FontWeight.w600,
                    color: Colors.white,
                  ),
                ).paddingOnly(left: 20.w),
              ),
              AppGaps.vGap12,
             GetBuilder(
               init: controller,
               builder: (_) {
                 return  Align(
                   alignment: Alignment.center,
                   child: SizedBox(
                     width: 77.w,
                     height: 77.w,
                     child: controller.entity != null ?  CircleAvatar(
                       backgroundImage: NetworkImage(
                           controller.entity!.faceUrl.startsWith('http') ?
                           '${controller.entity?.faceUrl}' : '${Interface.fileUrl}${controller.entity?.faceUrl}'
                       ),
                     ).clickShowLarge(
                       url: controller.entity!.faceUrl.startsWith('http') ?
                       '${controller.entity?.faceUrl}' : '${Interface.fileUrl}${controller.entity?.faceUrl}',
                     ) : Container(
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(50.w),
                        ),
                     ),
                   ),
                 );
               },
             ),
              AppGaps.vGap10,
              Align(
                  alignment: Alignment.center,
                  child: Text(
                    UserManager.getUser()?.systemUser?.nickname ?? '',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  )),
              AppGaps.vGap20,
              _buildCard(
                '个人信息',
                AppImages.icon_me_profile,
                AppImages.icon_me_right_arrow1,
                () {
                  Get.toNamed(AppRoutes.myProfile);
                },
              ),
              _buildCard(
                '资质证照',
                AppImages.icon_me_cert,
                AppImages.icon_me_right_arrow3,
                () {
                  Get.toNamed(AppRoutes.myCert);
                },
              ),
              _buildCard(
                '培训档案',
                AppImages.icon_me_assess,
                AppImages.icon_me_right_arrow2,
                () {
                  Get.toNamed(AppRoutes.myAssessment);
                },
              ),
              AppGaps.vGap120,
              Align(
                alignment: Alignment.center,
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.blue, width: 1.w),
                    borderRadius: BorderRadius.circular(37.w),
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 53.w, vertical: 11.w),
                  child: Text(
                    '退出登录',
                    style: TextStyle(
                      fontSize: 16.w,
                      color: Colors.blue,
                    ),
                  ),
                ).clickOnce(UserManager.logout),
              ),
              AppGaps.vGap100
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildCard(
    String title,
    String icon,
    String iconBottom,
    void Function() onTap,
  ) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.w),
        gradient: const LinearGradient(
          colors: [
            Color(0xFFE5EDFF),
            Colors.white,
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
        boxShadow: [
          BoxShadow(
            color: const Color(0x140545C7),
            offset: Offset(0, 3.w),
            blurRadius: 4.w,
          ),
        ],
      ),
      margin: EdgeInsets.symmetric(vertical: 10.w, horizontal: 20.w),
      padding: EdgeInsets.all(17.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                title,
                style: TextStyle(
                  fontSize: 16.w,
                  color: const Color(0xFF3D3D3D),
                  fontWeight: FontWeight.w500,
                ),
              ),
              AppGaps.vGap16,
              SvgPicture.asset(
                iconBottom,
                width: 20.w,
                height: 20.w,
              ),
            ],
          ),
          SizedBox(
            width: 50.w,
            height: 50.w,
            child: SvgPicture.asset(icon),
          ),
        ],
      ),
    ).clickOnce(onTap);
  }
}
